//
//  ZJTabBarController.m
//  报销封装
//
//  Created by ZJ on 17/3/20.
//
//

#import "ZJTabBarController.h"
#import "MyBranchViewController.h"
#import "UIColor+additions.h"
#import "ZJNavigationController.h"
#import "JPUSHService.h"
#import "ZJFingerprintViewController.h"
#import "ZJPoliticsBirthdayView.h"
/** 我的历程 */
#import "MyBranchPartyFootPrintViewController.h"
/** 签到 */
#import "ZJTodaySingWebViewController.h"
#import "ZJBBranchViewController.h"
@interface ZJTabBarController ()<ZJPoliticsBirthdayViewDelegate>

@end

@implementation ZJTabBarController
{
    ZJPoliticsBirthdayView *_birthdayView;//入党时间比对
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ZJBBranchViewController *homeVc = [[ZJBBranchViewController alloc] init];
    homeVc.type = EBranchType;
    [self addChildVc:homeVc title:@"支部" image:@"tab_zhibu_d" selectedImage:@"tab_zhibu_h"];
    

    MyBranchViewController *addressBookVc = [[MyBranchViewController alloc] init];
    [self addChildVc:addressBookVc title:@"党员通" image:@"tab_home_d" selectedImage:@"tab_home_h"];

 
    

    
    [self setupTabBar];
  
    NSMutableArray *array = [NSMutableArray array];
    if (ZJUSERINFORMODEL.DEPT_MODEL_JOB) {
        [array addObject:ZJUSERINFORMODEL.DEPT_MODEL_JOB.ID];
    }
    if (ZJUSERINFORMODEL.DEPT_MODEL_BOOK) {
        [array addObject:ZJUSERINFORMODEL.DEPT_MODEL_BOOK.ID];
    }
    NSSet *set = [[NSSet alloc]initWithArray:array];
    [JPUSHService setTags:set alias:ZJUSERINFORMODEL.ORG_ACCOUNT.ID callbackSelector:nil object:nil];
    [self addNotifacation];
    [self judgeBirthday];
    [self jpush];
}

#pragma mark - 添加通知
- (void)addNotifacation{
    //添加指纹识别通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_Fingerprint) name:FINGERPRINT object:nil];
    //注册赚取分享点通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_shareMore) name:SHAREMORE object:nil];
    //双向认领通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_clamAndOrder) name:CLAMANDORDER object:nil];


}

#pragma mark - 极光推送
- (void)jpush{
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功：%@",registrationID);
            if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"]) {
                NSMutableArray *array = [NSMutableArray array];
                if (ZJUSERINFORMODEL.DEPT_MODEL_JOB) {
                    [array addObject:ZJUSERINFORMODEL.DEPT_MODEL_JOB.ID];
                }
                if (ZJUSERINFORMODEL.DEPT_MODEL_BOOK) {
                    [array addObject:ZJUSERINFORMODEL.DEPT_MODEL_BOOK.ID];
                }
                NSSet *set = [[NSSet alloc]initWithArray:array];
                [JPUSHService setTags:set alias:ZJUSERINFORMODEL.ORG_ACCOUNT.ID callbackSelector:nil object:nil];
            }
        }
        else{
            NSLog(@"registrationID获取失败，code：%d",resCode);
        }
    }];
    
}

#pragma mark - 双向认领通知
- (void)action_clamAndOrder{
    self.selectedIndex = 1;
}
#pragma mark - 分享点通知
- (void)action_shareMore{
    self.selectedIndex = 0;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [[NSNotificationCenter defaultCenter] postNotificationName:SHAREMORE_ object:nil];
//    });
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - 指纹识别
- (void)action_Fingerprint{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:FINGER] isEqualToString:@"1"]) {
        [self Fingerprint];
    }
}

- (void)Fingerprint{
    ZJFingerprintViewController *vc = [ZJFingerprintViewController new];
    vc.view.frame = [UIScreen mainScreen].bounds;
    [self.view addSubview:vc.view];
    [self addChildViewController:vc];
    
}


- (void)setupTabBar
{
    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    bgView.backgroundColor = [UIColor whiteColor];
    self.selectedIndex = 1;
//    self.viewControllers.firstObject.tabBarItem.badgeValue = @"111";
    self.tabBar.opaque = YES;
}


- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor zj_colorWithR:123 G:123 B:123 Al:1];
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor themeColor];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    ZJNavigationController *nav = [[ZJNavigationController alloc] initWithRootViewController:childVc];
    nav.navigationBar.hidden = YES;
    [self addChildViewController:nav];

}



#pragma mark - 入党日期
//判断是否弹框
- (void)judgeBirthday{
    //当前时间
    NSString *currTime = [NSDateFormatter localStringOnlyMonthAndDayWithDate:[NSDate date]];
    //入党时间
    NSString *BirthdayTime = [NSDateFormatter localStringOnlyMonthAndDayWithDate:[NSDateFormatter dateWithString:ZJUSERINFORMODEL.ORG_ACCOUNT.JOINMONTH]];
    if ([currTime isEqualToString:BirthdayTime]) {
        [self showBirthdayView:[NSDateFormatter localStringWithDate:[NSDateFormatter dateWithString:ZJUSERINFORMODEL.ORG_ACCOUNT.JOINMONTH]]];
    }
    
}
///入党时间比对
- (void)showBirthdayView:(NSString *)time{
    _birthdayView = [[ZJPoliticsBirthdayView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _birthdayView.time.text = time;
    _birthdayView.delegate = self;
    [self.view addSubview:_birthdayView];

}
- (void)ZJPoliticsBirthdayViewDelegate_myCourse{
    MyBranchPartyFootPrintViewController *vc = [MyBranchPartyFootPrintViewController new];
    vc.type = @"我的历程";
    vc.accoundId = ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID;
    [_birthdayView removeFromSuperview];
    [self.selectedViewController pushViewController:vc animated:YES];
}
- (void)ZJPoliticsBirthdayViewDelegate_time{
    ZJTodaySingWebViewController *vc = [ZJTodaySingWebViewController new];
    [_birthdayView removeFromSuperview];
    [self.selectedViewController pushViewController:vc animated:YES];
}
- (void)ZJPoliticsBirthdayViewDelegate_canle{
    [_birthdayView removeFromSuperview];
}




@end
